#![allow(dead_code, mutable_transmutes, non_camel_case_types, non_snake_case, non_upper_case_globals, unused_assignments, unused_mut)]
#![feature(extern_types)]
extern crate libc;
use primary::alloc_testing::*;
use primary::trie::*;
use primary::compare_pointer::*;
use primary::compare_string::*;
use primary::compare_int::*;
use primary::hash_pointer::*;
use primary::hash_string::*;
use primary::hash_int::*;
extern "C" {
    pub type _Trie;
    fn sprintf(_: *mut libc::c_char, _: *const libc::c_char, _: ...) -> libc::c_int;
    fn memset(
        _: *mut libc::c_void,
        _: libc::c_int,
        _: libc::c_ulong,
    ) -> *mut libc::c_void;
    fn strcmp(_: *const libc::c_char, _: *const libc::c_char) -> libc::c_int;
    fn __assert_fail(
        __assertion: *const libc::c_char,
        __file: *const libc::c_char,
        __line: libc::c_uint,
        __function: *const libc::c_char,
    ) -> !;
                                                }
#[no_mangle]
pub static mut test_array: [libc::c_int; 10000] = [0; 10000];
#[no_mangle]
pub static mut test_strings: [[libc::c_char; 10]; 10000] = [[0; 10]; 10000];
#[no_mangle]
pub static mut bin_key: [libc::c_uchar; 7] = [
    'a' as i32 as libc::c_uchar,
    'b' as i32 as libc::c_uchar,
    'c' as i32 as libc::c_uchar,
    0 as libc::c_int as libc::c_uchar,
    1 as libc::c_int as libc::c_uchar,
    2 as libc::c_int as libc::c_uchar,
    0xff as libc::c_int as libc::c_uchar,
];
#[no_mangle]
pub static mut bin_key2: [libc::c_uchar; 8] = [
    'a' as i32 as libc::c_uchar,
    'b' as i32 as libc::c_uchar,
    'c' as i32 as libc::c_uchar,
    0 as libc::c_int as libc::c_uchar,
    1 as libc::c_int as libc::c_uchar,
    2 as libc::c_int as libc::c_uchar,
    0xff as libc::c_int as libc::c_uchar,
    0 as libc::c_int as libc::c_uchar,
];
#[no_mangle]
pub static mut bin_key3: [libc::c_uchar; 3] = [
    'a' as i32 as libc::c_uchar,
    'b' as i32 as libc::c_uchar,
    'c' as i32 as libc::c_uchar,
];
#[no_mangle]
pub static mut bin_key4: [libc::c_uchar; 4] = [
    'z' as i32 as libc::c_uchar,
    0 as libc::c_int as libc::c_uchar,
    'z' as i32 as libc::c_uchar,
    'z' as i32 as libc::c_uchar,
];
#[no_mangle]
pub extern "C" fn generate_trie() -> *mut Trie {
    let mut trie: *mut Trie = std::ptr::null_mut();
    let mut i: libc::c_int = 0;
    let mut entries: libc::c_uint = 0;

    unsafe {
        trie = trie_new();
    }

    entries = 0;
    i = 0;
    while i < 10000 {
        unsafe {
            test_array[i as usize] = i;
            sprintf(
                (test_strings[i as usize]).as_mut_ptr(),
                b"%i\0" as *const u8 as *const libc::c_char,
                i,
            );
            assert_ne!(
                trie_insert(
                    trie,
                    (test_strings[i as usize]).as_mut_ptr(),
                    &mut *test_array.as_mut_ptr().offset(i as isize) as *mut libc::c_int
                        as TrieValue,
                ),
                0,
                "trie_insert(trie, test_strings[i], &test_array[i]) != 0"
            );
        }

        entries = entries.wrapping_add(1);
        assert_eq!(
            unsafe { trie_num_entries(trie) },
            entries,
            "trie_num_entries(trie) == entries"
        );
        i += 1;
    }
    trie
}
#[no_mangle]
pub extern "C" fn test_trie_new_free() {
    let mut trie: *mut Trie = std::ptr::null_mut();

    
    unsafe {
        trie = trie_new();
        assert!(!trie.is_null(), "trie should not be NULL");
        trie_free(trie);
        trie = trie_new();
    }

    assert_ne!(
        unsafe {
            trie_insert(
                trie,
                b"hello\0" as *const u8 as *const libc::c_char as *mut libc::c_char,
                b"there\0" as *const u8 as *const libc::c_char as TrieValue,
            )
        },
        0,
        "trie_insert(trie, \"hello\", \"there\") should not return 0"
    );
    assert_ne!(
        unsafe {
            trie_insert(
                trie,
                b"hell\0" as *const u8 as *const libc::c_char as *mut libc::c_char,
                b"testing\0" as *const u8 as *const libc::c_char as TrieValue,
            )
        },
        0,
        "trie_insert(trie, \"hell\", \"testing\") should not return 0"
    );
    assert_ne!(
        unsafe {
            trie_insert(
                trie,
                b"testing\0" as *const u8 as *const libc::c_char as *mut libc::c_char,
                b"testing\0" as *const u8 as *const libc::c_char as TrieValue,
            )
        },
        0,
        "trie_insert(trie, \"testing\", \"testing\") should not return 0"
    );
    assert_ne!(
        unsafe {
            trie_insert(
                trie,
                b"\0" as *const u8 as *const libc::c_char as *mut libc::c_char,
                b"asfasf\0" as *const u8 as *const libc::c_char as TrieValue,
            )
        },
        0,
        "trie_insert(trie, \"\", \"asfasf\") should not return 0"
    );

    unsafe {
        trie_free(trie);
        trie = trie_new();
    }

    assert_ne!(
        unsafe {
            trie_insert(
                trie,
                b"hello\0" as *const u8 as *const libc::c_char as *mut libc::c_char,
                b"there\0" as *const u8 as *const libc::c_char as TrieValue,
            )
        },
        0,
        "trie_insert(trie, \"hello\", \"there\") should not return 0"
    );
    assert_ne!(
        unsafe {
            trie_remove(
                trie,
                b"hello\0" as *const u8 as *const libc::c_char as *mut libc::c_char,
            )
        },
        0,
        "trie_remove(trie, \"hello\") should not return 0"
    );

    unsafe {
        trie_free(trie);
    }
}
#[no_mangle]
pub extern "C" fn test_trie_insert() {
    let mut trie: *mut Trie = std::ptr::null_mut();
    let mut entries: libc::c_uint = 0;
    let mut allocated: libc::size_t = 0;

    unsafe {
        trie = generate_trie();
    }

    let trie_box = unsafe { Box::from_raw(trie) };

    entries = trie_num_entries(trie);

    assert_eq!(
        trie_insert(
            trie,
            b"hello world\0" as *const u8 as *const libc::c_char as *mut libc::c_char,
            0 as *mut libc::c_void,
        ),
        0
    );

    assert_eq!(trie_num_entries(trie), entries);

    trie = Box::into_raw(trie_box);

    unsafe {
        trie_free(trie);
    }
}
#[no_mangle]
pub extern "C" fn test_trie_lookup() {
    let mut trie: *mut Trie = std::ptr::null_mut();
    let mut buf: [libc::c_char; 10] = [0; 10];
    let mut val: *mut libc::c_int = std::ptr::null_mut();
    let mut i: libc::c_int = 0;

    unsafe {
        trie = generate_trie();
    }

    let trie_box = unsafe { Box::from_raw(trie) };

    assert!(
        unsafe {
            trie_lookup(
                Box::into_raw(Box::new(*trie_box)),
                b"000000000000000\0" as *const u8 as *const libc::c_char as *mut libc::c_char,
            )
        }
        .is_null(),
        "trie_lookup(trie, \"000000000000000\") == TRIE_NULL"
    );

    assert!(
        unsafe {
            trie_lookup(
                Box::into_raw(Box::new(*trie_box)),
                b"\0" as *const u8 as *const libc::c_char as *mut libc::c_char,
            )
        }
        .is_null(),
        "trie_lookup(trie, \"\") == TRIE_NULL"
    );

    i = 0;
    while i < 10000 {
        unsafe {
            sprintf(buf.as_mut_ptr(), b"%i\0" as *const u8 as *const libc::c_char, i);
        }

        val = unsafe { trie_lookup(Box::into_raw(Box::new(*trie_box)), buf.as_mut_ptr()) } as *mut libc::c_int;

        assert_eq!(unsafe { *val }, i, "*val == i");

        i += 1;
    }

    trie = Box::into_raw(trie_box);

    unsafe {
        trie_free(trie);
    }
}
#[no_mangle]
pub extern "C" fn test_trie_remove() {
    let mut trie: *mut Trie = std::ptr::null_mut();
    let mut buf: [libc::c_char; 10] = [0; 10];
    let mut i: libc::c_int = 0;
    let mut entries: libc::c_uint = 0;

    unsafe {
        trie = generate_trie();
    }

    let trie_box = unsafe { Box::from_raw(trie) };

    assert_eq!(
        trie_remove(
            trie,
            b"000000000000000\0" as *const u8 as *const libc::c_char as *mut libc::c_char,
        ),
        0,
        "trie_remove(trie, \"000000000000000\") == 0"
    );

    assert_eq!(
        trie_remove(trie, b"\0" as *const u8 as *const libc::c_char as *mut libc::c_char),
        0,
        "trie_remove(trie, \"\") == 0"
    );

    entries = trie_num_entries(trie);
    assert_eq!(
        entries,
        10000,
        "entries == NUM_TEST_VALUES"
    );

    i = 0;
    while i < 10000 {
        unsafe {
            sprintf(buf.as_mut_ptr(), b"%i\0" as *const u8 as *const libc::c_char, i);
        }

        assert_ne!(
            trie_remove(trie, buf.as_mut_ptr()),
            0,
            "trie_remove(trie, buf) != 0"
        );

        entries = entries.wrapping_sub(1);
        assert_eq!(
            trie_num_entries(trie),
            entries,
            "trie_num_entries(trie) == entries"
        );

        i += 1;
    }

    trie = Box::into_raw(trie_box);
    unsafe {
        trie_free(trie);
    }
}
#[no_mangle]
pub extern "C" fn test_trie_replace() {
    let mut trie: *mut Trie = std::ptr::null_mut();
    let mut val: *mut libc::c_int = std::ptr::null_mut();

    unsafe {
        trie = generate_trie();
    }

    unsafe {
        val = alloc_test_malloc(std::mem::size_of::<libc::c_int>() as libc::c_ulong) as *mut libc::c_int;
    }

    unsafe {
        *val = 999;
    }

    let trie_box = unsafe { Box::from_raw(trie) };
    let val_box = unsafe { Box::from_raw(val) };

    assert_ne!(
        trie_insert(
            trie,
            b"999\0" as *const u8 as *const libc::c_char as *mut libc::c_char,
            val as TrieValue,
        ),
        0,
        "trie_insert(trie, \"999\", val) != 0"
    );

    assert_eq!(
        trie_num_entries(trie),
        10000,
        "trie_num_entries(trie) == NUM_TEST_VALUES"
    );

    assert_eq!(
        trie_lookup(
            trie,
            b"999\0" as *const u8 as *const libc::c_char as *mut libc::c_char,
        ),
        val as TrieValue,
        "trie_lookup(trie, \"999\") == val"
    );

    unsafe {
        alloc_test_free(val as *mut libc::c_void);
    }

    trie = Box::into_raw(trie_box);
    val = Box::into_raw(val_box);

    unsafe {
        trie_free(trie);
    }
}
#[no_mangle]
pub extern "C" fn test_trie_insert_empty() {
    let mut trie: *mut Trie = std::ptr::null_mut();
    let mut buf: [libc::c_char; 10] = [0; 10];

    unsafe {
        trie = trie_new();
    }

    let trie_box = unsafe { Box::from_raw(trie) };

    assert_ne!(
        trie_insert(
            trie,
            b"\0" as *const u8 as *const libc::c_char as *mut libc::c_char,
            buf.as_mut_ptr() as TrieValue,
        ),
        0,
        "trie_insert(trie, \"\", buf) != 0"
    );

    assert_ne!(
        trie_num_entries(trie),
        0,
        "trie_num_entries(trie) != 0"
    );

    assert_eq!(
        trie_lookup(
            trie,
            b"\0" as *const u8 as *const libc::c_char as *mut libc::c_char,
        ),
        buf.as_mut_ptr() as TrieValue,
        "trie_lookup(trie, \"\") == buf"
    );

    assert_ne!(
        trie_remove(
            trie,
            b"\0" as *const u8 as *const libc::c_char as *mut libc::c_char,
        ),
        0,
        "trie_remove(trie, \"\") != 0"
    );

    assert_eq!(
        trie_num_entries(trie),
        0,
        "trie_num_entries(trie) == 0"
    );

    trie = Box::into_raw(trie_box);

    unsafe {
        trie_free(trie);
    }
}
extern "C" fn test_trie_free_long() {
    let mut long_string: *mut libc::c_char = std::ptr::null_mut();
    let mut trie: *mut Trie = std::ptr::null_mut();

    unsafe {
        long_string = alloc_test_malloc(4096) as *mut libc::c_char;
        memset(long_string as *mut libc::c_void, 'A' as i32, 4096);
        *long_string.offset(4095) = '\0' as i32 as libc::c_char;
        trie = trie_new();
    }

    let trie_box = unsafe { Box::from_raw(trie) };
    trie_insert(trie, long_string, long_string as TrieValue);
    trie = Box::into_raw(trie_box);

    unsafe {
        trie_free(trie);
        alloc_test_free(long_string as *mut libc::c_void);
    }
}
extern "C" fn test_trie_negative_keys() {
    let mut my_key: [libc::c_char; 6] = [
        'a' as i32 as libc::c_char,
        'b' as i32 as libc::c_char,
        'c' as i32 as libc::c_char,
        -(50 as libc::c_int) as libc::c_char,
        -(20 as libc::c_int) as libc::c_char,
        '\0' as i32 as libc::c_char,
    ];
    let mut trie: *mut Trie = std::ptr::null_mut();
    let mut value: *mut libc::c_void = std::ptr::null_mut();

    trie = trie_new();
    let trie_box = unsafe { Box::from_raw(trie) };

    assert_ne!(
        trie_insert(
            trie,
            my_key.as_mut_ptr(),
            b"hello world\0" as *const u8 as *const libc::c_char as TrieValue,
        ),
        0,
        "trie_insert(trie, my_key, \"hello world\") != 0"
    );

    value = trie_lookup(trie, my_key.as_mut_ptr());
    assert_eq!(
        unsafe { strcmp(value as *const libc::c_char, b"hello world\0" as *const u8 as *const libc::c_char) },
        0,
        "!strcmp(value, \"hello world\")"
    );

    assert_ne!(
        trie_remove(trie, my_key.as_mut_ptr()),
        0,
        "trie_remove(trie, my_key) != 0"
    );

    assert_eq!(
        trie_remove(trie, my_key.as_mut_ptr()),
        0,
        "trie_remove(trie, my_key) == 0"
    );

    assert!(
        (trie_lookup(trie, my_key.as_mut_ptr())).is_null(),
        "trie_lookup(trie, my_key) == NULL"
    );

    trie = Box::into_raw(trie_box);
    trie_free(trie);
}
#[no_mangle]
pub extern "C" fn generate_binary_trie() -> *mut Trie {
    let mut trie: *mut Trie = std::ptr::null_mut();

    trie = trie_new();
    let trie_box = unsafe { Box::from_raw(trie) };

    assert!(
        trie_insert_binary(
            trie,
            unsafe { bin_key2.as_mut_ptr() },
            std::mem::size_of::<[libc::c_uchar; 8]>() as libc::c_ulong as libc::c_int,
            b"goodbye world\0" as *const u8 as *const libc::c_char as TrieValue,
        ) != 0,
        "trie_insert_binary(trie, bin_key2, sizeof(bin_key2), \"goodbye world\") != 0"
    );
    assert!(
        trie_insert_binary(
            trie,
            unsafe { bin_key.as_mut_ptr() },
            std::mem::size_of::<[libc::c_uchar; 7]>() as libc::c_ulong as libc::c_int,
            b"hello world\0" as *const u8 as *const libc::c_char as TrieValue,
        ) != 0,
        "trie_insert_binary(trie, bin_key, sizeof(bin_key), \"hello world\") != 0"
    );

    trie = Box::into_raw(trie_box);
    trie
}
#[no_mangle]
pub extern "C" fn test_trie_insert_binary() {
    let mut trie: *mut Trie = std::ptr::null_mut();
    let mut value: *mut libc::c_char = std::ptr::null_mut();

    unsafe {
        trie = generate_binary_trie();
    }

    let trie_box = unsafe { Box::from_raw(trie) };

    assert_ne!(
        trie_insert_binary(
            trie,
            unsafe { bin_key.as_mut_ptr() },
            std::mem::size_of::<[libc::c_uchar; 7]>() as libc::c_ulong as libc::c_int,
            b"hi world\0" as *const u8 as *const libc::c_char as TrieValue,
        ),
        0,
        "trie_insert_binary(trie, bin_key, sizeof(bin_key), \"hi world\") != 0"
    );

    assert_eq!(
        trie_insert_binary(
            trie,
            unsafe { bin_key3.as_mut_ptr() },
            std::mem::size_of::<[libc::c_uchar; 3]>() as libc::c_ulong as libc::c_int,
            std::ptr::null_mut(),
        ),
        0,
        "trie_insert_binary(trie, bin_key3, sizeof(bin_key3), NULL) == 0"
    );

    value = trie_lookup_binary(
        trie,
        unsafe { bin_key.as_mut_ptr() },
        std::mem::size_of::<[libc::c_uchar; 7]>() as libc::c_ulong as libc::c_int,
    ) as *mut libc::c_char;

    assert_eq!(
        unsafe { strcmp(value, b"hi world\0" as *const u8 as *const libc::c_char) },
        0,
        "!strcmp(value, \"hi world\")"
    );

    value = trie_lookup_binary(
        trie,
        unsafe { bin_key2.as_mut_ptr() },
        std::mem::size_of::<[libc::c_uchar; 8]>() as libc::c_ulong as libc::c_int,
    ) as *mut libc::c_char;

    assert_eq!(
        unsafe { strcmp(value, b"goodbye world\0" as *const u8 as *const libc::c_char) },
        0,
        "!strcmp(value, \"goodbye world\")"
    );

    trie = Box::into_raw(trie_box);

    unsafe {
        trie_free(trie);
    }
}
#[no_mangle]
pub unsafe extern "C" fn test_trie_insert_out_of_memory() {}
#[no_mangle]
pub extern "C" fn test_trie_remove_binary() {
    let mut trie: *mut Trie = std::ptr::null_mut();
    let mut value: *mut libc::c_void = std::ptr::null_mut();

    
    unsafe {
        trie = generate_binary_trie();
    }

    
    unsafe {
        value = trie_lookup_binary(
            trie,
            bin_key3.as_mut_ptr(),
            std::mem::size_of::<[libc::c_uchar; 3]>() as libc::c_ulong as libc::c_int,
        );
    }
    assert!(value.is_null(), "value == NULL");

    
    unsafe {
        assert_eq!(
            trie_remove_binary(
                trie,
                bin_key3.as_mut_ptr(),
                std::mem::size_of::<[libc::c_uchar; 3]>() as libc::c_ulong as libc::c_int,
            ),
            0,
            "trie_remove_binary(trie, bin_key3, sizeof(bin_key3)) == 0"
        );
    }

    
    unsafe {
        assert!(
            trie_lookup_binary(
                trie,
                bin_key4.as_mut_ptr(),
                std::mem::size_of::<[libc::c_uchar; 4]>() as libc::c_ulong as libc::c_int,
            )
            .is_null(),
            "trie_lookup_binary(trie, bin_key4, sizeof(bin_key4)) == 0"
        );
    }

    
    unsafe {
        assert_eq!(
            trie_remove_binary(
                trie,
                bin_key4.as_mut_ptr(),
                std::mem::size_of::<[libc::c_uchar; 4]>() as libc::c_ulong as libc::c_int,
            ),
            0,
            "trie_remove_binary(trie, bin_key4, sizeof(bin_key4)) == 0"
        );
    }

    
    unsafe {
        assert_ne!(
            trie_remove_binary(
                trie,
                bin_key2.as_mut_ptr(),
                std::mem::size_of::<[libc::c_uchar; 8]>() as libc::c_ulong as libc::c_int,
            ),
            0,
            "trie_remove_binary(trie, bin_key2, sizeof(bin_key2)) != 0"
        );
    }

    
    unsafe {
        assert!(
            trie_lookup_binary(
                trie,
                bin_key2.as_mut_ptr(),
                std::mem::size_of::<[libc::c_uchar; 8]>() as libc::c_ulong as libc::c_int,
            )
            .is_null(),
            "trie_lookup_binary(trie, bin_key2, sizeof(bin_key2)) == NULL"
        );
    }

    
    unsafe {
        assert!(
            !trie_lookup_binary(
                trie,
                bin_key.as_mut_ptr(),
                std::mem::size_of::<[libc::c_uchar; 7]>() as libc::c_ulong as libc::c_int,
            )
            .is_null(),
            "trie_lookup_binary(trie, bin_key, sizeof(bin_key)) != NULL"
        );
    }

    
    unsafe {
        assert_ne!(
            trie_remove_binary(
                trie,
                bin_key.as_mut_ptr(),
                std::mem::size_of::<[libc::c_uchar; 7]>() as libc::c_ulong as libc::c_int,
            ),
            0,
            "trie_remove_binary(trie, bin_key, sizeof(bin_key)) != 0"
        );
    }

    
    unsafe {
        assert!(
            trie_lookup_binary(
                trie,
                bin_key.as_mut_ptr(),
                std::mem::size_of::<[libc::c_uchar; 7]>() as libc::c_ulong as libc::c_int,
            )
            .is_null(),
            "trie_lookup_binary(trie, bin_key, sizeof(bin_key)) == NULL"
        );
    }

    
    unsafe {
        trie_free(trie);
    }
}
